package com.ls.x.dao;

import android.database.Cursor;
import android.database.sqlite.SQLiteDatabase;

import com.ls.x.activity.beans.ImageBean;
import com.ls.x.tool.DatabaseHelper;

public class ImageDao implements IImageDao {
	private DatabaseHelper databaseHelper;
	private Cursor cursor = null;

	// 获取ImageDao实例的时候，提供如下构造方法，该方法需要传入一个DatabaseHelper对象
	public ImageDao(DatabaseHelper databaseHelper) {
		super();
		this.databaseHelper = databaseHelper;
	}

	private SQLiteDatabase db = null;

	@Override
	public boolean insertImage(ImageBean imageBean) {
		// TODO Auto-generated method stub
		db.execSQL("insert into image(image_path,image_note,context_id) values('"
				+ imageBean.getImagePath()
				+ "','"
				+ imageBean.getImageNote()
				+ "'," + imageBean.getContextId() + ")");
		return true;
	}

	@Override
	public boolean deleteImageByPath(String imagePath) {
		// TODO Auto-generated method stub
		db.execSQL("delete from image where image_path =" + imagePath + ")");
		return true;
	}

	@Override
	public boolean updateImageNoteByPath(String imageNote, String imagePath) {
		// TODO Auto-generated method stub
		db.execSQL("update image set image_note='" + imageNote
				+ "' where image_path =" + imagePath + ")");
		return true;
	}

	@Override
	public ImageBean selectImageByContextId(Long contextId) {
		// TODO Auto-generated method stub
		ImageBean imageBean = new ImageBean();
		cursor = db.rawQuery("select * from image where context_id="
				+ contextId, null);
		if (cursor.moveToNext()) {
			imageBean.setContextId(contextId);
			imageBean.setImagePath(cursor.getString(cursor
					.getColumnIndex("image_path")));
			imageBean.setImageNote(cursor.getString(cursor
					.getColumnIndex("image_note")));
			imageBean.setId(Integer.parseInt(cursor.getString(cursor
					.getColumnIndex("id"))));
			return imageBean;
		}
		return null;
	}
}
